export default {
  data() {
    return {
      userName: '',//姓名
      userMobile: '',//电话
      addCarShow: false, // 显示添加车辆弹框
      scanFlag: false, //扫描标识s
      sacnType: '', //shot-车牌 vin-vin
      carInfo: {},
      keyboardShow: false, //车牌键盘
      abbrName: '川', //车牌前
      carNum: '', //车牌后
      lyBDOCR: null, //图片识别插件
      platform: '', //手机系统
      //添加爱车列表
      specifAll: {
			  brandlist: [], //品牌
			  Serieslist: [], //系列
			  Displacement: [], //排量
			  Modelyear: [], //年款
			  Configulist: [], //车子
      },
      isComfin: false,
      id: '',//福利购id
    }
  },

  onLoad(op) {
    this.id = op.id
    console.log(this.id)
    // #ifdef APP-PLUS
    //引用插件
    this.lyBDOCR = uni.requireNativePlugin('longyoung-BDOCR')
    this.platform = uni.getSystemInfoSync().platform
    // #endif

    //手动选择车型监听
    uni.$on('createCar', data => {
      Object.assign(this.specifAll, data)
      console.log(this.specifAll,'all')
    })
  },

  onShow() {

  },
  onUnload() {
	  uni.$off('createCar')
  },
  methods: {
    close(){
      this.isComfin = false
      uni.navigateBack({
        delta: 1
      })
    },
    pay(){
      this.addBuyDiscountsOrders()
    },
    //提交提单
    async addBuyDiscountsOrders() {
		  try {
		    let data = {
		      'id': this.id
		    }
		    const res = await this.$api.maintenanceIndex.addBuyDiscountsOrders(data)

		    let list = res
		    if (res.code === 200) {
		      uni.navigateTo({
		        url: '/pages_commodity/payment/payment?orderNo=' + list.data + '&price=' + this.list.discount_Price +
							'&type=buyDiscountsOrder'
		      })
		    }
		  } catch (e) {
		    //TODO handle the exception
		  }

    },
    //添加爱车
    goBrand(){
      uni.navigateTo({
			  url: '/price_Comparison/addYourCar/addYourCar?type=120'+'&pageFrom=createCar'+'&isShow=0'
      })
    },
    //提交
    confirm(){
      if(!this.userMobile){
			  uni.showToast({
				  title: '请输入电话号码',
				  icon: 'none',
			  })
        return false
      }else if(!this.carInfo.vin){
			  uni.showToast({
			    title: '请输入vin',
			    icon: 'none',
			  })
        return false
      }else if(!this.carNum){
			  uni.showToast({
			    title: '请输入车牌',
			    icon: 'none',
			  })
        return false
      }else if (!/^[A-HJ-NPR-Z\d]{17}$/.test(this.carInfo.vin)) {
        uni.showToast({
				  title: '请填写正确的VIN',
				  icon: 'none',
        })
			  return false
      }else if(!this.specifAll.brandlist.bid){
			  uni.showToast({
			    title: '请选择品牌',
			    icon: 'none',
			  })
			  return false
      }else{
        this.isComfin = true
      }

    },
    // 添加车辆 ， 选择添加方式
    selectAddType() {
      if (this.hadCarNumber) {
        //车辆列表
        uni.navigateTo({
          url: '/pages_afterSales/myLoveCar/myLoveCar',
        })
      } else {
        this.addCarShow = true
      }
    },

    // 跳转到添加/切换车辆页面，选择车辆
    goSelectCarPage() {
      this.addCarShow = false
      uni.navigateTo({
        url: '/pages_afterSales/addYourCar/addYourCar',
      })
    },

    // 跳转到相机扫描
    goScan(type) {
      // #ifdef APP-PLUS
      this.scanType = type
      this.scanFlag = true
      let param = type ? `?type=${type}` : ''
      uni.navigateTo({
        url: '/pages_common/scan/scan' + param,
      })
      // #endif
      // #ifndef APP-PLUS
      uni.showToast({
        title: '浏览器暂不支持该功能',
        icon: 'none',
      })

      // #endif
    },

    //扫描读取图片信息(用插件读取图片信息)
    readPic(type) {
      this.lyBDOCR.startOCRLy(
        {
          type: type,
          accuracy: 'auto', //支持这三个字符串：auto、normal、high，精准度，精度越高，速度越慢。default：auto
        },
        result => {
          // console.log(result,'vin')
          uni.showLoading({
            title: '识别中',
          })
          let number = ''
          if (this.platform === 'ios') {
            number = type === 107 ? result.result[0].words : result.number
          } else {
            number = result.text ? result.text.replace(/[\n]/g, '') : ''
          }
          uni.hideLoading()
          if (number.length) {
            if (type === 107) {
              // this.carInfo.vin = number.slice(0, 17)
              this.$set(this.carInfo, 'vin', number.slice(0, 17))
              // this.getCarInfoByVin(this.carInfo.vin)
            } else {
              this.$set(this.carInfo, 'carNum', number.slice(0, 8))
              this.abbrName = number.slice(0, 1)
              this.carNum = number.slice(1, 8)
            }
          }
        }
      )
    },

    //vin获取车辆信息
    getCarInfoByVin(vin) {
      this.$api.maintenanceIndex
        .getCarInfoByVin({ vin })
        .then(res => {
          console.log(res.data)
          if (res.data.carName) {
            this.$set(this.carInfo, 'carName', res.data.carName)
            if (res.data.brandImg) {
              this.$set(this.carInfo, 'brandImg', res.data.brandImg)
            }
          }
          if (res.data.engine) {
            this.$set(this.carInfo, 'engine', res.data.engine)
          }
          this.$set(this.carInfo, 'vin', vin)
        })
        .catch(e => {
          console.log(e)
        })
    },

    //选择车牌
    keyboardChange(e) {
      this.abbrName = e
      this.keyboardShow = false
      this.carInfo.carNum = this.abbrName + this.carNum
    },

    //填写车牌号失焦
    changeCarNum() {
      this.carInfo.carNum = this.abbrName + this.carNum
    },

    vincheck() {
      this.carInfo.vin = this.carInfo.vin.toUpperCase()
      const exp = /^[A-HJ-NPR-Z\d]{17}$/
      if (!exp.test(this.carInfo.vin)) {
        uni.showToast({
          title: '请输入正确的vin码',
          icon: 'none',
        })
      }
    },
  },
}
